package top.dotgo.web.base.config.jackson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import top.dotgo.kit.CfgKit;

import java.util.Date;

/**
 * @author : lin
 * @date : 2019/10/9 14:50
 */
class JacksonMapper extends ObjectMapper {


    JacksonMapper() {
        super();
        this.configure(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, true);
        this.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
        this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        if (CfgKit.isDebug()) {
            this.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        }

        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        simpleModule.addSerializer(long.class, ToStringSerializer.instance);
        registerModule(simpleModule);

        simpleModule = new SimpleModule();
        simpleModule.addSerializer(Date.class, DateToStringSerializer.INSTANCE);
        registerModule(simpleModule);

    }
}
